package com.rabbitmq.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/confirm")
@Slf4j
public class ConfirmCallBackController {

    public static final String CONFIRM_EXCHANGE_NAME = "confirm.exchange";

    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMessage/{message}")
    public void sendMessage(@PathVariable String message){

        //指定消息 id 为 1
        CorrelationData correlationData1=new CorrelationData("1");
        String routingKey_1="key1";

        rabbitTemplate.convertAndSend(CONFIRM_EXCHANGE_NAME,routingKey_1,message+routingKey_1,correlationData1);
        log.info("发送消息内容:{}",message);


        CorrelationData correlationData2 = new CorrelationData("2");
        String routingKey_2 ="key2";

        rabbitTemplate.convertAndSend(CONFIRM_EXCHANGE_NAME,routingKey_2,message+routingKey_2,correlationData2);
        log.info("发送消息内容:{}",message);
    }
}

